﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaoNiuOA.Models;

namespace TaoNiuOA.Web.Controllers
{
    public class UserGroupController : Controller
    {
        private DbEntity db = new DbEntity();

        [IsAdmin]
        public ViewResult Index()
        {
            var list = db.UserGroups.OrderBy(n => n.Sort).ToList();
            return View(list);
        }
        [IsAdmin]
        public ActionResult Create()
        {
            return View();
        }

        //添加职位
        [IsAdmin]
        [HttpPost]
        public ActionResult Create(UserGroup usergroup)
        {
            if (ModelState.IsValid)
            {            
                //判断职位是否已经存在
                foreach (var ug in db.UserGroups)
                {
                    if (ug.GroupName == usergroup.GroupName)
                    {
                        ModelState.AddModelError("GroupName","该职位已经存在!");
                        return View(usergroup);
                    }
                }

                if (usergroup.JobMoney < 0)
                {
                    ModelState.AddModelError("JobMoney", "岗位工资要大于0");
                    return View(usergroup);
                }

                if (usergroup.BasePoint < 0)
                {
                    ModelState.AddModelError("BasePoint", "基本绩效要大于0");
                    return View(usergroup);
                }

                db.UserGroups.Add(usergroup);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(usergroup);
        }

        [IsAdmin]
        public ActionResult Edit(int id)
        {
            UserGroup usergroup = db.UserGroups.Find(id);
            return View(usergroup);
        }

        [IsAdmin]
        [HttpPost]
        public ActionResult Edit(UserGroup usergroup)
        {
            if (ModelState.IsValid)
            {
                //判断名称是否已经存在
                foreach (var ug in db.UserGroups)
                {
                    if (ug.GroupName == usergroup.GroupName && ug.UserGroupID != usergroup.UserGroupID)
                    {
                        ModelState.AddModelError("GroupName", "该职位已经存在!");
                        return View(usergroup);
                    }
                }

                if (usergroup.JobMoney < 0)
                {
                    ModelState.AddModelError("JobMoney", "岗位工资要大于0");
                    return View(usergroup);
                }

                if (usergroup.BasePoint < 0)
                {
                    ModelState.AddModelError("BasePoint", "基本绩效要大于0");
                    return View(usergroup);
                }

                //编辑
                var sql = string.Format("update [usergroups] set [GroupName]='{0}',JobMoney='{1}',BasePoint='{2}' where [UserGroupID]='{3}'",
                    usergroup.GroupName,usergroup.JobMoney,usergroup.BasePoint, usergroup.UserGroupID);
                var n = db.Database.ExecuteSqlCommand(sql);
                return RedirectToAction("Index");
            }
            return View(usergroup);
        }

        [IsAdmin]
        public ActionResult Delete(int id)
        {
            UserGroup usergroup = db.UserGroups.Find(id);
            return View(usergroup);
        }

        [IsAdmin]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            UserGroup usergroup = db.UserGroups.Find(id);
            db.UserGroups.Remove(usergroup);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}